package com.planet.api;

import com.planet.facade.AssessmentUserBiz;
import com.planet.facade.CheckUserBiz;
import com.planet.facade.HouseholdBiz;
import com.planet.query.AssessmentUserQuery;
import com.planet.query.CheckUserQuery;
import com.planet.utils.AssertUtils;
import com.planet.utils.global.ExceptionHandle;
import com.planet.utils.global.Result;
import com.planet.utils.global.ResultUtil;
import com.planet.vo.AssessmentUserVO;
import com.planet.vo.CheckUserVO;
import com.planet.vo.HouseholdVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.text.AbstractDocument;
import java.util.Date;
import java.util.List;


/**
 * Created by jiayazi on 17/11/13.
 */
@Api("抽查")
@RestController
@RequestMapping("nb/api/check")
public class CheckAPI {
    private static final Logger LOGGER = LogManager.getLogger(CheckAPI.class);

    @Autowired
    private ExceptionHandle exceptionHandle;
    @Autowired
    private CheckUserBiz checkUserBiz;
    @Autowired
    AssessmentUserBiz assessmentUserBiz;
    @Autowired
    HouseholdBiz householdBiz;


    /**
     * 抽查插入
     * @param checkUserQuery 用户Id
     * @return
     */
    @PostMapping("/insertCheckInfo")
    @ApiOperation(value = "抽查插入")
    public Result insertCheckInfo(@ApiParam("插入信息") CheckUserQuery checkUserQuery){

        //默认结果是成功的
        Result result = ResultUtil.success();
        checkUserQuery.setCheck_time(new Date());
        AssertUtils.isNotNull(checkUserQuery.getPhone(),"手机号不能为空");
        HouseholdVO householdVO = householdBiz.selectByPhone(checkUserQuery.getPhone());
        if (StringUtils.isEmpty(householdVO.getPhone())){
            result = ResultUtil.error(1,"没有该督办用户，请检查手机号");
            return result;
        }


        try {
            checkUserQuery.setName(householdVO.getName());
            checkUserQuery.setAddress(householdVO.getAddress());
            checkUserQuery.setState(householdVO.getState());
            boolean state = checkUserBiz.insertCheckInfo(checkUserQuery);
            if (state){
                AssessmentUserQuery assessmentUserQuery = new AssessmentUserQuery();
                assessmentUserQuery.setAddress(checkUserQuery.getAddress());
                assessmentUserQuery.setName(checkUserQuery.getName());
                assessmentUserQuery.setPhone(checkUserQuery.getPhone());
                assessmentUserQuery.setCheck_time(checkUserQuery.getCheck_time());
                assessmentUserQuery.setCheck_txt(checkUserQuery.getCheck_txt());
                assessmentUserQuery.setCheck_img(checkUserQuery.getCheck_img());
                assessmentUserQuery.setCheck_video(checkUserQuery.getCheck_video());
                assessmentUserQuery.setCheck_file(checkUserQuery.getCheck_file());
                assessmentUserQuery.setAsssee_type(0);
                assessmentUserQuery.setIsEmpty(checkUserQuery.getIsEmpty());
                assessmentUserQuery.setClassification_type(checkUserQuery.getClassification_type());
                assessmentUserQuery.setDevice_meg(checkUserQuery.getDevice_meg());
                assessmentUserQuery.setFmily_health(checkUserQuery.getFmily_health());
                assessmentUserQuery.setState(checkUserQuery.getState());


                assessmentUserQuery.setMonth_fraction(10.0);
                assessmentUserQuery.setQuarter_fraction(20.0);
                assessmentUserQuery.setYear_fraction(30.0);
                assessmentUserQuery.setTotal_fraction(80.0);

                boolean dbState =  assessmentUserBiz.insertSelective(assessmentUserQuery);
                LOGGER.info("插入督办状态 === "+dbState);
            }

            //抽查成功加入已督办里面
            result =  ResultUtil.success(state);

        }catch (Exception e){
            //调用异常工具类，统一处理异常
            result =  exceptionHandle.exceptionGet(e);
        }
        return result;
    }




    /**
     * 根据编号查看抽查详情
     * @param phone 垃圾桶编号Id
     * @return
     */
    @GetMapping("/getCheckDetail")
    @ApiOperation(value = "根据编号查看抽查详情")
    public Result getCheckInfo(@ApiParam("phone") @RequestParam String phone){

        //默认结果是成功的
        Result result = ResultUtil.success();
        try {
            AssessmentUserVO assessmentUserVO = assessmentUserBiz.selectByPhoneAndType(phone);
            result =  ResultUtil.success(assessmentUserVO);

        }catch (Exception e){
            //调用异常工具类，统一处理异常
            result =  exceptionHandle.exceptionGet(e);
        }
        return result;
    }




    /**
     *获取抽查列表
     * @param
     * @return
     */
    @GetMapping("/getCheckList")
    @ApiOperation(value = "获取抽查列表")
    public Result getCheckList(@ApiParam("pageSize") @RequestParam String pageSize,
                               @ApiParam("pageIndex") @RequestParam String pageIndex){

        //默认结果是成功的
        Result result = ResultUtil.success();
        try {
            List<CheckUserVO> checkUserVOS = checkUserBiz.getCheckList();
            result =  ResultUtil.success(checkUserVOS);

        }catch (Exception e){
            //调用异常工具类，统一处理异常
            result =  exceptionHandle.exceptionGet(e);
        }
        return result;
    }

}
